package com.lst.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lst.api.entity.LoanBank;
import com.lst.api.entity.Product;
import com.lst.api.service.ILoanBankService;
import com.lst.api.service.IProductService;
import com.lst.api.vo.response.MatchProductResponse;
import com.lst.api.vo.response.ProductResponse;
import com.lst.tools.custom.annotation.TokenPermission;
import com.lst.tools.custom.vo.JsonResponse;
import com.lst.tools.web.annotation.WebLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 贷款产品表控制层
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Api(tags = "贷款产品表控制层")
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private ILoanBankService loanBankService;

    @ApiOperation(value = "根据id查看贷款产品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "access-token", value = "登录token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "productId", value = "产品id", required = true, dataType = "long")
    })
    @GetMapping(value = "/getInfo")
    @TokenPermission
    @WebLog
    public JsonResponse<ProductResponse> getInfo(@RequestParam(value = "productId") long productId) {
        Product product = productService.getById(productId);
        if (product == null) {
            return JsonResponse.success();
        }
        ProductResponse productResponse = new ProductResponse(product);
        LoanBank loanBank = loanBankService.getById(product.getLoanBankId());
        productResponse.setLoanBankName(loanBank.getTitle());
        return JsonResponse.success(productResponse);
    }

    @ApiOperation(value = "根据申请编号获取匹配的产品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "access-token", value = "登录token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "applyNo", value = "申请编号", required = true, dataType = "String")
    })
    @GetMapping(value = "/listMatchProductByApplyNo")
    @TokenPermission
    @WebLog
    public JsonResponse<List<MatchProductResponse>> listMatchProductByApplyNo(@RequestParam(value = "applyNo") String applyNo) {
        List<MatchProductResponse> matchProductList = productService.listMatchProductByApplyNo(applyNo);
        return JsonResponse.success(matchProductList);
    }
}